/**
  ******************************************************************************
  * @file    Icons.c
  * @author  Qilong Huang
  * @version V0.0.1
  * @date    05-03-2020
  * @brief   This file contains functions enable/disable  icons
  *          
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; KION(SZ) R&D Center
  *
  * Licensed under KION, (the "License");
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/


#include "icons.h"
#include "Key.h"

U8 led_control = 0x00;
u32 icon_out_1 = 0;
u8 led_counter = 0;
u8 fix_led_counter = 0;
u8 bat_led_counter = 0;
u8 Buz_10Sec_flag = TRUE;
u8 Buz_5Sec_flag = TRUE;
int buz_counter = 0;
int buz_counter_0 = 0;
/**
* @brief	all led on
* @param	none
* @note     none
* @retval   none
*/
void All_Led_On(void)
{
	U8 temp_out = 0x00;
	temp_out = temp_out | LED_STOP_ON | LED_BATTTARYLOW_ON | LED_TEMPURATEHIGH_ON | LED_FIX_ON | LED_DRIVERON_ON;
    write_sl595(temp_out);
}
/**
* @brief	all led off 
* @param	none
* @note     none
* @retval   none
*/
void All_Led_Off(void)
{
    led_control = 0;
    write_sl595(0);
}


/**
* @brief	write s595, digital output to control led or others 
* @param	u16 data: output data
* @note     none
* @retval   none
*/
void write_sl595(u32 data)
{
    u16 i, data1temp, value;
    
    value = data & 0xFF;
    GPIO_ResetBits(IC595_LATCH_PORT, IC595LATCH_PIN);
    for(i = 0; i < DIG_1_OUTPUTS_NUM; i++)   
    {
        GPIO_ResetBits(DIG_OUTPUT_PORTS, CHIP_1_CLK_PIN);
        Delay(5);
        data1temp = (value >> i) & 0x01;
        if(data1temp == 0x00)
        {
            GPIO_SetBits(DIG_OUTPUT_PORTS, CHIP_1_DATA_PIN);
        }
        else
        {
            GPIO_ResetBits(DIG_OUTPUT_PORTS, CHIP_1_DATA_PIN);
        }
        Delay(5);
        GPIO_SetBits(DIG_OUTPUT_PORTS, CHIP_1_CLK_PIN);
        Delay(5);
    }
    GPIO_SetBits(IC595_LATCH_PORT, IC595LATCH_PIN);
}
/**
* @brief	This task updates the status of the Led Icons based on the configured 
*           inputs (switches, CAN, etc.)
* @param	void const *argument
* @note     none
* @retval   none
*/
void ledicons_task(void const *argument) 
{
    u32 temp_out = 0;
    // Initialization 
    task_status[UPDT_LEDICONS_ID] = ALIVE;
    // Tasks must run in an infinite loop:
	GPIO_ResetBits(DIG_OUTPUT_PORTS, CHIP_1_OUTPUT_EN_PIN);            //enable 1595 cs pin
    while (TRUE)
    {
        temp_out = 0x00;
        switch(Test_step_counter)
        {
            case 3: temp_out = temp_out | LED_DRIVERON_ON; break;
            case 4: temp_out = temp_out | LED_FIX_ON; break;
            case 5: temp_out = temp_out | LED_TEMPURATEHIGH_ON; break;
            case 6: temp_out = temp_out | LED_BATTTARYLOW_ON; break;
            case 7: temp_out = temp_out | LED_STOP_ON; break;
            case 19: Buzzer_ON(); break;
            default: Buzzer_OFF(); break;
        }    
		write_sl595(temp_out);
        task_status[UPDT_LEDICONS_ID] = ALIVE;
        osDelay(UPDT_LEDICONS_ITV);
    }  // end while infinite loop
}  // end task_updt_icons()

